﻿using System;
using System.Collections.Generic;
using System.Data;
using Jin.Core.DataProviders;

namespace Jin.Core.Schema
{
    
   /// <summary>
   /// 数据列
   /// </summary>
   public class DatabaseColumn:IColumn
   {
       public class ReservedColumnName
       {
           public const string CREATED_BY = "CreatedBy";
           public const string CREATED_ON = "CreatedOn";
           public const string DELETED = "Deleted";
           public const string IS_ACTIVE = "IsActive";
           public const string IS_DELETED = "IsDeleted";
           public const string MODIFIED_BY = "ModifiedBy";
           public const string MODIFIED_ON = "ModifiedOn";
       }
       #region 构造函数
       
       public DatabaseColumn() { }
       public DatabaseColumn(ITable tbl):this(string.Empty,tbl)
       { 
       }
       public DatabaseColumn(string columnName,ITable tbl)
           {
               Table = tbl;
               Name = columnName;
           }
       #endregion

       #region IColumn 接口成员
       public bool IsForeignKey { get; set; }
       public ITable Table { get; set; }
       public DbType DataType { get; set; }
       public int MaxLength { get; set; }
       public bool IsNullable { get; set; }
       public bool IsReadOnlu { get; set; }
       public bool IsComputed { get; set; }
       public bool AutoIncrement { get; set; }
       public int NumberScale { get; set; }
       public int NumericPrecision { get; set; }
       public bool IsPrimaryKey { get; set; }
       public object DefaultSetting { get; set; }
       public string SchemaName
       {
           get { return Table.SchemaName; }
           set { Table.SchemaName = value; }
       }
       /// <summary>
       /// 数据提供驱动
       /// </summary>
       public IDataProvider Provider
       {
           get { return Table.Provider; }
           set { Table.Provider = value; }
       }
       /// <summary>
       /// 名称
       /// </summary>
       public string Name { get; set; }
       /// <summary>
       /// 属性名称
       /// </summary>
       public string PropertyName { get; set; }
       /// <summary>
       /// 参数名称
       /// </summary>
       public string ParameterName {
           get {
               const string paramFormat = "{0}{1}";
               return string.Format(paramFormat, Table.Provider.ParameterPrefix, PropertyName);
           }
       }
       /// <summary>
       /// 合法名
       /// </summary>
       public string QualifiedName
       {
           get { return Table.Provider.QualifyColumnName(this); }
       }
       /// <summary>
       /// 外键指向
       /// </summary>
       public ITable ForeignKeyTo { get; set; }
       /// <summary>
       /// 友好名
       /// </summary>
       public string FriendlyName { get; set; }
       /// <summary>
       /// 是否是数字（包括有小数点的数字）
       /// </summary>
       public bool IsNumeric
       {
           get
           {
               return DataType == DbType.Currency ||
                      DataType == DbType.Decimal ||
                      DataType == DbType.Double ||
                      DataType == DbType.Int16 ||
                      DataType == DbType.Int32 ||
                      DataType == DbType.Int64 ||
                      DataType == DbType.UInt16 ||
                      DataType == DbType.UInt32 ||
                      DataType == DbType.UInt64 ||
                      DataType == DbType.VarNumeric;
           
           }
       }

       /// <summary>
       /// 是否是日期格式
       /// </summary>
       public bool IsDateTime
       {
           get { return DataType==DbType.DateTime||
                        DataType==DbType.Time||
                        DataType==DbType.Date;
           
           }

       }

       /// <summary>
       /// 是否是字符串
       /// </summary>
       public  bool IsString
       {
           get
           {
               return DataType == DbType.AnsiString ||
                       DataType == DbType.AnsiStringFixedLength ||
                       DataType == DbType.String ||
                       DataType == DbType.StringFixedLength;
           
           }
       }

       /// <summary>
       /// 创建列sql
       /// </summary>
       public string CreateSql
       {
           get {return Provider.SchemaGeneretor.BuildAddColumnStatement(Table.Name, this); }
       }
       /// <summary>
       /// 跟新列sql
       /// </summary>
       public string AlterSql
       {
           get { return Provider.SchemaGeneretor.BuildAlterColumnStatement(this); }   
       }
       /// <summary>
       /// 删除列sql
       /// </summary>
       public string DeleteSql
       {
           get { return Provider.SchemaGeneretor.BuildDorpColumnStatement(Table.Name, Name); }
       }
       #endregion

       /// <summary>
       /// 重写 ToString方法返回列名
       /// </summary>
       /// <returns>列名</returns>
       public override string ToString()
       {
           return Name;
       }
       /// <summary>
       /// 重写Equals方法
       /// 主键不比较、字符串比较类型和长度
       /// 数字比较类型、精度、取值范围
       /// </summary>
       /// <param name="obj"></param>
       /// <returns></returns>
       public override bool Equals(object obj)
       {
           if(obj.GetType()==typeof(IColumn))//是否是列
           {
               DatabaseColumn compareTo = (DatabaseColumn)obj;
               if (IsPrimaryKey)
                   return true;//不更新主键
               if (DataType == DbType.String) /*比较数据类型和长度*/
                   return compareTo.DataType == DataType && MaxLength == compareTo.MaxLength;
               if (IsNumeric)/*比较数据类型、长度、精度*/
               {
                   return compareTo.DataType == DataType &&
                       NumericPrecision == compareTo.NumericPrecision &&
                       NumberScale == compareTo.NumberScale;
               }
               return compareTo.DataType == DataType;
           }
           return base.Equals(obj);
       }

       public override int GetHashCode()
       {
           return base.GetHashCode();
       }
   }
}
